var currentPage = 1;
var count = 3;
var search;
var ModuleId;
var pages;
$(function () {


    getRemoteAdmin(currentPage, count, search, ModuleId);


//    上一页链接的点击时间
    $('#prev').click(function () {
        --currentPage;
        if (currentPage < 1) {
            currentPage = 1
        }
        getRemoteAdmin(currentPage, count, search, ModuleId)
    })
// 下一页链接的点击事件


    $('#next').click(function () {
        ++currentPage;
        if (currentPage > pages) {
            currentPage = 1;
        }
        getRemoteAdmin(currentPage, count, search, ModuleId)
    })

// 下拉选框的事件
    $('#selModules').change(function () {
        ModuleId = this.value;
        getRemoteAdmin(currentPage, count, search, ModuleId)
    })



// 搜索按钮的点击事件
    $('.btn_search').click(function () {

        search = $('#search').val()

        getRemoteAdmin(currentPage, count, search, ModuleId)
    })


// 向网站发送请求,传递一个page参数
    function getRemoteAdmin(page, count, searchMsg, ModuleId) {
        page = +page <= 0 ? 1 : +page;
        count = +count <= 0 ? 3 : +count;

        var params = {
            page: page,
            count: count,
            search: searchMsg,
            moduleId : ModuleId
        }
        $.get('/admin/adminList', params, handlerResponse)

    }

// 请求的回调函数,请求回来的数据都在参数 showDetails 中
    function handlerResponse(resp) {
        if (resp.status === 200) {
            var totalCount = resp.data.totalCount;
            //  计算一共需要分多少页
            pages = Math.ceil(totalCount / count);
            // 调用画页码的函数,传递需要画多少页的参数 pages
            drawPageIndicator(pages);

            // 获取到请求到的需要铺在页面上的数据
            var items = resp.data.items;

            console.log("总条数是:" + totalCount)
            console.log("满足条件的是" + items.length)
            // 调用渲染表格的方法,把请求到的数据的items 当参数传递过去
            putItemsInTable(items);
        }

    }

// 画页码的函数
    function drawPageIndicator(pages) {
        console.log('pages' + pages)
        var ul = $('#page')
        ul.empty();

        for (var i = 1; i <= pages; i++) {
            var li = $('<li style="float: left"></li>');
            // console.log(li)


            var a = $('<a></a>');
            // 给a标签添加属性,这条属性的意思为点击链接后不进行页面上的跳转.
            a.attr('href', 'javascript:void(0)');
            a.text(i);


            if (currentPage == i) {
                a.addClass('current_page');
            }

            a.click(onIndicatorClick)

            li.append(a);
            ul.append(li);
        }
    }

// 当前的页面的点击事件
    function onIndicatorClick() {
        currentPage = +$(this).text();

        search = $('#search').val();

        getRemoteAdmin(currentPage, count, search, ModuleId);
    }

// 将数据渲染到表格的函数
    function putItemsInTable(items) {

        var tbody = $('#tbody');
        tbody.empty();

        for (var i of items) {
            var tr = $('<tr></tr>')
            var td1 = $('<td></td>')
            var input = $('<input type="checkbox"></input>')
            td1.append(input)

            var td_adminId = $('<td id="adminId"></td>').text(i.adminId);
            var td_adminName = $('<td></td>').text(i.adminName);
            var td_adminCode = $('<td></td>').text(i.adminCode);
            var td_telephone = $('<td></td>').text(i.telephone);
            var td_email = $('<td></td>').text(i.email);
            var td_enrolldate = $('<td></td>').text(i.enrolldate);

            // 浮动信息框的实现
            let allRole = i.allRole;
            if (allRole.length > 5){
                allRole = allRole.substring(0,5) + "...";
            }
            var td_allRole = $('<td></td>');
            var summary = $('<a class="summary" onmouseover="showDetail(true,this);" onmouseout="showDetail(false,this);"></a>').html(allRole)
            var detail_info = $('<div class="detail_info"></div>').html(i.allRole)
            td_allRole.append(summary).append(detail_info);


            var td_modi = $('<td class="td_modi"></td>')
            var modi_btn = $('<input type="button" value="修改" class="btn_modify" />')
            var del_btn = $(' <input type="button" value="删除" class="btn_delete" />')
            td_modi.append(modi_btn).append(del_btn);

            tr.append(td1)
            tr.append(td_adminId).append(td_adminName).append(td_adminCode)
                .append(td_telephone).append(td_email).append(td_enrolldate)
                .append(td_allRole).append(td_modi)
            tbody.append(tr)


        }
    }



    $("body").on("click", "input.btn_modify", function () {
        var adminId = $(this).parents("tr").children().eq(1).html();
        var adminName = $(this).parents("tr").children().eq(2).html();
        var adminCode = $(this).parents("tr").children().eq(3).html();
        var telephone = $(this).parents("tr").children().eq(4).html();
        var email = $(this).parents("tr").children().eq(5).html();


        window.localStorage.setItem("adminId", adminId)
        window.localStorage.setItem("adminName", adminName)
        window.localStorage.setItem("adminCode", adminCode)
        window.localStorage.setItem("telephone", telephone)
        window.localStorage.setItem("email", email)



        window.location.href = '/admin/admin_modi.html'
    })


    $("body").on("click", "input.btn_delete", function () {
        var param = $(this).parents("tr").children().eq(1).html();
        var r = window.confirm("确定要删除此管理员吗？");
        if (r == true) {
            $.get("/admin/adminDel?adminId=" + param)

            window.location.reload();
        } else {
            console.log('你取消了这次操作')
        }

    })

})
